<?php
/**
 * CGridEventHandler class file.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.vasconcelossa.com.br/yap/license
 * @link http://www.vasconcelossa.com.br/yap/
 * @version $Id: CGridEventHandler.php 20 2010-02-08 20:31:41Z contato@lucasvasconcelos.com $
 * @package yap.web.ui
 */

Prado::using('yap.web.ui.interfaces');

/**
 * CGridEventHandler class.
 * 
 * Manipuladores de evento padr�o usados pela classe CPageGrid.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision: 20 $
 * @package yap.web.ui
 */
class CGridEventHandler
{
	/**
	 * Responde ao evento OnSetupGrid.
	 *
	 * Configura propriedades e manipuladores de evento para o componente TDataGrid requerido por IPageGrid.
	 * Alguns par�metros podem ser configurados via arquivo XML (applications.xml ou config.xml).
	 * 
	 *  * PageSize -- Quantidade de resultados por página, exibidos na grid.
	 *  * PagePosition -- Posi��o do componente de pagina��o (Bottom, Top, TopAndBottom).
	 *  * PagerButtonCount -- N�mero de p�ginas exibidas no componente pagina��o.
	 *
	 */
	public static function setupGrid(IPageGrid $sender,$param)
	{
		$sender->{IPageGrid::GRID_ID}->setAutoGenerateColumns(false);
		$sender->{IPageGrid::GRID_ID}->setCellSpacing(0);
		$sender->{IPageGrid::GRID_ID}->setCellPadding(0);
		$sender->{IPageGrid::GRID_ID}->getAlternatingItemStyle()->setCssClass('alt');
		$sender->{IPageGrid::GRID_ID}->setAllowSorting(true);
		$sender->{IPageGrid::GRID_ID}->setAllowPaging(true);
		$sender->{IPageGrid::GRID_ID}->setPageSize(Prado::getApplication()->Parameters['PageSize']);
		$sender->{IPageGrid::GRID_ID}->getPagerStyle()->setPosition(Prado::getApplication()->Parameters['PagerPosition']);
		$sender->{IPageGrid::GRID_ID}->getPagerStyle()->setPageButtonCount(Prado::getApplication()->Parameters['PagerButtonCount']);
		$sender->{IPageGrid::GRID_ID}->getPagerStyle()->setMode('Numeric');
		$sender->{IPageGrid::GRID_ID}->getPagerStyle()->setCssClass('pager');
		
		// registry event handlers to deal with TDataGrid events.
		$sender->{IPageGrid::GRID_ID}->OnPagerCreated=array($sender->getGridManager(), 'pagerCreated');
		$sender->{IPageGrid::GRID_ID}->OnPageIndexChanged=array($sender->getGridManager(), 'pageIndexChanged');
		$sender->{IPageGrid::GRID_ID}->OnSortCommand=array($sender->getGridManager(), 'sortCommand');
	}
	
	/**
	 * Responde ao evento OnSetupCriteria.
	 *
	 * Configura objeto TActiveRecordCriteria e o registra na sess�o.
	 */
	public static function setupCriteria(IPageGrid $sender,$param)
	{
		Prado::trace('GridEventHandler setupCriteria()','yap.web.ui');
		$criteria=$sender->getGridManager()->getCriteria();//new TActiveRecordCriteria;
		$criteria->setCondition($sender->getCriteriaCondition());
		$sender->getGridManager()->setCriteria($criteria);
	}
	
	/**
	 * Responde ao evento OnLoadGrid.
	 * 
	 * Carrega dados para o objeto TDataGrid.
	 */
	public static function loadGrid(IPageGrid $sender,$param)
	{
		$sender->{IPageGrid::GRID_ID}->setDataSource($sender->getData($sender->getGridManager()->getCriteria()));
		$sender->{IPageGrid::GRID_ID}->dataBind();
	}
}
